package cn.stronglink.asset.module.basicdata.assetTypeAttrGroup.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetTypeAttr;
import cn.stronglink.asset.model.AssetTypeAttrGroup;
import cn.stronglink.asset.module.basicdata.assetTypeAttr.mapper.AssetTypeAttrMapper;
import cn.stronglink.asset.module.basicdata.assetTypeAttrGroup.mapper.AssetTypeAttrGroupMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class AssetTypeAttrGroupService {

	@Autowired
	private AssetTypeAttrGroupMapper assetModelMapper;
	@Autowired
	private AssetTypeAttrMapper assetTypeAttrMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetTypeAttrGroup> queryAssetTypeAttrGroup(Map<String, Object>  params) {
		List<AssetTypeAttrGroup> list =assetModelMapper.queryAssetTypeAttrGroup(params);	
		if(list!=null&&list.size()>0){
				Map<String, Object> map =new HashMap<String, Object>();
				map.put("assetTypeId", params.get("assetTypeId"));
				map.put("groupId", list.get(0).getId());
				List<AssetTypeAttr> attrList = assetTypeAttrMapper.queryAssetTypeAttr(map);
				list.get(0).setAttrList(attrList);
		}
		return list;	
	}
	
	public void removeAssetTypeAttrGroup(Long id){
		assetModelMapper.removeAssetTypeAttrGroup(id);	
	}	

	public void insertAssetTypeAttrGroup(AssetTypeAttrGroup assetTypeAttrGroup) {
		if (assetTypeAttrGroup.getId() == null) {
			assetTypeAttrGroup.setId(IdWorker.getId());
			assetTypeAttrGroup.setCreateBy(WebUtil.getCurrentUser());
			assetTypeAttrGroup.setCreateTime(new Date());
			assetModelMapper.insertAssetTypeAttrGroup(assetTypeAttrGroup);		
		}else{
			assetTypeAttrGroup.setUpdateBy(WebUtil.getCurrentUser());
			assetTypeAttrGroup.setUpdateTime(new Date());
			assetModelMapper.updateAssetTypeAttrGroup(assetTypeAttrGroup);
		}	
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public AssetTypeAttrGroup qryById(Long id) {
		return assetModelMapper.qryById(id);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Integer getGroupAttr(Long id) {
		return assetModelMapper.getGroupAttr(id);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetTypeAttrGroup> qryDetailByTypeId(Long typeId) {
		List<AssetTypeAttrGroup> list = assetModelMapper.getGroupByTypeId(typeId);
		List<AssetTypeAttrGroup> listCpy =new ArrayList<AssetTypeAttrGroup>();
		if(list!=null&&list.size()>0){
			for(AssetTypeAttrGroup group:list){
				Map<String, Object> map =new HashMap<String, Object>();
				map.put("assetTypeId", typeId);
				map.put("groupId", group.getId());
				List<AssetTypeAttr> attrList = assetTypeAttrMapper.queryAssetTypeAttrDisplay(map);
				if(attrList!=null&&attrList.size()>0){
					group.setAttrList(attrList);
					listCpy.add(group);
				}				
			}
		}
		return listCpy;
	}

}
